package br.com.vendaext.gerencial.aop;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ExceptionAdvice {
	// @AfterThrowing(pointcut =
	// "execution(* br.com.vendaext.gerencial.integracao.*.*(..))", throwing =
	// "e")
	// public void tratarExcecao(Exception e) {
	// try {
	// FacesContext context = FacesContext.getCurrentInstance();
	// context.addMessage(null, new FacesMessage(e.getMessage()));
	// } catch (Exception e2) {
	//
	// }
	//
	// }

	@Around("br.com.vendaext.gerencial.aop.ArquiteturaAOP.inIntegracaoLayer() && @annotation(HideException)")
	public Object hideIntegracaoException(ProceedingJoinPoint pjp) {
		// long start = System.currentTimeMillis();
		// System.out.println("Going to call the method.");
		try {
			Object output = pjp.proceed();
			return output;
		} catch (Throwable e) {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(e.getMessage()));
			return null;
		}

		// System.out.println("Method execution completed.");
		// long elapsedTime = System.currentTimeMillis() - start;
		// System.out.println("Method execution time: " + elapsedTime +
		// " milliseconds.");

	}
}
